package com.cpone.cloudweb.utils.bak;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

  
public class DateUtils {  
  
  
    public static final String YYYYMMDD = "yyyy-MM-dd";  
  
    public static final String YYYYMMDD_ZH = "yyyy��MM��dd��";  
  
    public static final int FIRST_DAY_OF_WEEK = Calendar.MONDAY; // �й���һ��һ�ܵĵ�һ��   
  
    /** 
     *  
     * @param strDate 
     * @return 
     */  
    public static Date parseDate(String strDate) {  
        return parseDate(strDate, null);  
    }  
  
    /** 
     * parseDate 
     *  
     * @param strDate 
     * @param pattern 
     * @return 
     */  
    public static Date parseDate(String strDate, String pattern) {  
        Date date = null;  
        try {  
            if (pattern == null) {  
                pattern = YYYYMMDD;  
            }  
            SimpleDateFormat format = new SimpleDateFormat(pattern);  
            date = format.parse(strDate);  
        } catch (Exception e) {  
        	e.printStackTrace();
        }  
        return date;  
    }  
  
    /** 
     * format date 
     *  
     * @param date 
     * @return 
     */  
    public static String formatDate(Date date) {  
        return formatDate(date, null);  
    }  
  
    /** 
     * format date 
     *  
     * @param date 
     * @param pattern 
     * @return 
     */  
    public static String formatDate(Date date, String pattern) {  
        String strDate = null;  
        try {  
            if (pattern == null) {  
                pattern = YYYYMMDD;  
            }  
            SimpleDateFormat format = new SimpleDateFormat(pattern);  
            strDate = format.format(date);  
        } catch (Exception e) {  
        	e.printStackTrace();
        }  
        return strDate;  
    }  
  
    /** 
     * ȡ�����ڣ��� 
     *  
     * @param date 
     * @return 
     */  
    public static int getYear(Date date) {  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        int year = c.get(Calendar.YEAR);  
        return year;  
    }  
  
    /** 
     * ȡ�����ڣ��� 
     *  
     * @param date 
     * @return 
     */  
    public static int getMonth(Date date) {  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        int month = c.get(Calendar.MONTH);  
        return month + 1;  
    }  
  
    /** 
     * ȡ�����ڣ��� 
     *  
     * @param date 
     * @return 
     */  
    public static int getDay(Date date) {  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        int da = c.get(Calendar.DAY_OF_MONTH);  
        return da;  
    }  
  
    /** 
     * ȡ�õ����������ܼ� 
     *  
     * @param date 
     * @return 
     */  
    public static int getWeekDay(Date date) {  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        int week_of_year = c.get(Calendar.DAY_OF_WEEK);  
        return week_of_year - 1;  
    }  
  
    /** 
     * ȡ��һ��ĵڼ��� 
     *  
     * @param date 
     * @return 
     */  
    public static int getWeekOfYear(Date date) {  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        int week_of_year = c.get(Calendar.WEEK_OF_YEAR);  
        return week_of_year;  
    }  
  
    /** 
     * getWeekBeginAndEndDate 
     *  
     * @param date 
     * @param pattern 
     * @return 
     */  
    public static String getWeekBeginAndEndDate(Date date, String pattern) {  
        Date monday = getMondayOfWeek(date);  
        Date sunday = getSundayOfWeek(date);  
        return formatDate(monday, pattern) + " - "  
                + formatDate(sunday, pattern);  
    }  
  
    /** 
     * ��������ȡ�ö�Ӧ����һ���� 
     *  
     * @param date 
     * @return 
     */  
    public static Date getMondayOfWeek(Date date) {  
        Calendar monday = Calendar.getInstance();  
        monday.setTime(date);  
        monday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);  
        monday.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);  
        return monday.getTime();  
    }  
  
    /** 
     * ��������ȡ�ö�Ӧ���������� 
     *  
     * @param date 
     * @return 
     */  
    public static Date getSundayOfWeek(Date date) {  
        Calendar sunday = Calendar.getInstance();  
        sunday.setTime(date);  
        sunday.setFirstDayOfWeek(FIRST_DAY_OF_WEEK);  
        sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);  
        return sunday.getTime();  
    }  
  
    /** 
     * ȡ���µ�ʣ������ 
     *  
     * @param date 
     * @return 
     */  
    public static int getRemainDayOfMonth(Date date) {  
        int dayOfMonth = getDayOfMonth(date);  
        int day = getPassDayOfMonth(date);  
        return dayOfMonth - day;  
    }  
  
    /** 
     * ȡ�����Ѿ��������� 
     *  
     * @param date 
     * @return 
     */  
    public static int getPassDayOfMonth(Date date) {  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        return c.get(Calendar.DAY_OF_MONTH);  
    }  
  
    /** 
     * ȡ�������� 
     *  
     * @param date 
     * @return 
     */  
    public static int getDayOfMonth(Date date) {  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        return c.getActualMaximum(Calendar.DAY_OF_MONTH);  
    }  
  
    /** 
     * ȡ���µ�һ�� 
     *  
     * @param date 
     * @return 
     */  
    public static Date getFirstDateOfMonth(Date date) {  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));  
        return c.getTime();  
    }  
  
    /** 
     * ȡ�������һ�� 
     *  
     * @param date 
     * @return 
     */  
    public static Date getLastDateOfMonth(Date date) {  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));  
        return c.getTime();  
    }  
  
    /**
     * ��ȡ��һ�µ�һ��
     * @param date
     * @return
     */
    public static Date getNextMonthFirstDayOfMonth(Date date){
    	Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	c.add(Calendar.MONTH, 1);
    	c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
    	return c.getTime();
    }
    
    /**
     * ��ȡ��һ�������
     * @param date
     * @return
     */
    public static Date getPrevMonthLastDayOfMonth(Date date){
    	Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	c.add(Calendar.MONTH, -1);
    	c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
    	return c.getTime();
    }
    
    
    /** 
     * ȡ�ü��ȵ�һ�� 
     *  
     * @param date 
     * @return 
     */  
    public static Date getFirstDateOfSeason(Date date) {  
        return getFirstDateOfMonth(getSeasonDate(date)[0]);  
    }  
  
    /** 
     * ȡ�ü������һ�� 
     *  
     * @param date 
     * @return 
     */  
    public static Date getLastDateOfSeason(Date date) {  
        return getLastDateOfMonth(getSeasonDate(date)[2]);  
    }  
  
    /** 
     * ȡ�ü������� 
     *  
     * @param date 
     * @return 
     */  
    public static int getDayOfSeason(Date date) {  
        int day = 0;  
        Date[] seasonDates = getSeasonDate(date);  
        for (Date date2 : seasonDates) {  
            day += getDayOfMonth(date2);  
        }  
        return day;  
    }  
  
    /** 
     * ȡ�ü���ʣ������ 
     *  
     * @param date 
     * @return 
     */  
    public static int getRemainDayOfSeason(Date date) {  
        return getDayOfSeason(date) - getPassDayOfSeason(date);  
    }  
  
    /** 
     * ȡ�ü����ѹ����� 
     *  
     * @param date 
     * @return 
     */  
    public static int getPassDayOfSeason(Date date) {  
        int day = 0;  
  
        Date[] seasonDates = getSeasonDate(date);  
  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        int month = c.get(Calendar.MONTH);  
  
        if (month == Calendar.JANUARY || month == Calendar.APRIL  
                || month == Calendar.JULY || month == Calendar.OCTOBER) {// ���ȵ�һ����   
            day = getPassDayOfMonth(seasonDates[0]);  
        } else if (month == Calendar.FEBRUARY || month == Calendar.MAY  
                || month == Calendar.AUGUST || month == Calendar.NOVEMBER) {// ���ȵڶ�����   
            day = getDayOfMonth(seasonDates[0])  
                    + getPassDayOfMonth(seasonDates[1]);  
        } else if (month == Calendar.MARCH || month == Calendar.JUNE  
                || month == Calendar.SEPTEMBER || month == Calendar.DECEMBER) {// ���ȵ�������   
            day = getDayOfMonth(seasonDates[0]) + getDayOfMonth(seasonDates[1])  
                    + getPassDayOfMonth(seasonDates[2]);  
        }  
        return day;  
    }  
  
    /** 
     * ȡ�ü����� 
     *  
     * @param date 
     * @return 
     */  
    public static Date[] getSeasonDate(Date date) {  
        Date[] season = new Date[3];  
  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
  
        int nSeason = getSeason(date);  
        if (nSeason == 1) {// ��һ����   
            c.set(Calendar.MONTH, Calendar.JANUARY);  
            season[0] = c.getTime();  
            c.set(Calendar.MONTH, Calendar.FEBRUARY);  
            season[1] = c.getTime();  
            c.set(Calendar.MONTH, Calendar.MARCH);  
            season[2] = c.getTime();  
        } else if (nSeason == 2) {// �ڶ�����   
            c.set(Calendar.MONTH, Calendar.APRIL);  
            season[0] = c.getTime();  
            c.set(Calendar.MONTH, Calendar.MAY);  
            season[1] = c.getTime();  
            c.set(Calendar.MONTH, Calendar.JUNE);  
            season[2] = c.getTime();  
        } else if (nSeason == 3) {// ��������   
            c.set(Calendar.MONTH, Calendar.JULY);  
            season[0] = c.getTime();  
            c.set(Calendar.MONTH, Calendar.AUGUST);  
            season[1] = c.getTime();  
            c.set(Calendar.MONTH, Calendar.SEPTEMBER);  
            season[2] = c.getTime();  
        } else if (nSeason == 4) {// ���ļ���   
            c.set(Calendar.MONTH, Calendar.OCTOBER);  
            season[0] = c.getTime();  
            c.set(Calendar.MONTH, Calendar.NOVEMBER);  
            season[1] = c.getTime();  
            c.set(Calendar.MONTH, Calendar.DECEMBER);  
            season[2] = c.getTime();  
        }  
        return season;  
    }  
  
    /** 
     *  
     * 1 ��һ���� 2 �ڶ����� 3 �������� 4 ���ļ��� 
     *  
     * @param date 
     * @return 
     */  
    public static int getSeason(Date date) {  
  
        int season = 0;  
  
        Calendar c = Calendar.getInstance();  
        c.setTime(date);  
        int month = c.get(Calendar.MONTH);  
        switch (month) {  
        case Calendar.JANUARY:  
        case Calendar.FEBRUARY:  
        case Calendar.MARCH:  
            season = 1;  
            break;  
        case Calendar.APRIL:  
        case Calendar.MAY:  
        case Calendar.JUNE:  
            season = 2;  
            break;  
        case Calendar.JULY:  
        case Calendar.AUGUST:  
        case Calendar.SEPTEMBER:  
            season = 3;  
            break;  
        case Calendar.OCTOBER:  
        case Calendar.NOVEMBER:  
        case Calendar.DECEMBER:  
            season = 4;  
            break;  
        default:  
            break;  
        }  
        return season;  
    }  
  
    public static void main(String[] args) {  
  
        String strDate = "2013-01-01";  
  
        Date date = parseDate(strDate);  
  
        System.out.println(strDate + " ��������һ�ꣿ" + getYear(date));  
        System.out.println(strDate + " �������ĸ��£�" + getMonth(date));  
        System.out.println(strDate + " �����Ǽ��ţ�" + getDay(date));  
        System.out.println(strDate + " �������ܼ���" + getWeekDay(date));  
        System.out.println(strDate + " ��һ��ĵڼ��ܣ�" + getWeekOfYear(date));  
        System.out.println(strDate + " ��������ʼ�������ڣ�"  
                + getWeekBeginAndEndDate(date, "yyyy��MM��dd��"));  
        System.out.println(strDate + " ��������һ�ǣ�"  
                + formatDate(getMondayOfWeek(date)));  
        System.out.println(strDate + " �����������ǣ�"  
                + formatDate(getSundayOfWeek(date)));  
  
        System.out.println(strDate + " ���µ�һ�����ڣ�"  
                + formatDate(getFirstDateOfMonth(date)));  
        System.out.println(strDate + " �������һ�����ڣ�"  
                + formatDate(getLastDateOfMonth(date)));  
        System.out.println(strDate + " ����������" + getDayOfMonth(date));  
        System.out.println(strDate + " �����ѹ������죿" + getPassDayOfMonth(date));  
        System.out.println(strDate + " ����ʣ������죿" + getRemainDayOfMonth(date));  
  
        System.out.println(strDate + " ���ڼ��ȵ�һ�����ڣ�"  
                + formatDate(getFirstDateOfSeason(date)));  
        System.out.println(strDate + " ���ڼ������һ�����ڣ�"  
                + formatDate(getLastDateOfSeason(date)));  
        System.out.println(strDate + " ���ڼ���������" + getDayOfSeason(date));  
        System.out.println(strDate + " ���ڼ����ѹ������죿" + getPassDayOfSeason(date));  
        System.out  
                .println(strDate + " ���ڼ���ʣ������죿" + getRemainDayOfSeason(date));  
        System.out.println(strDate + " �ǵڼ����ȣ�" + getSeason(date));  
        System.out.println(strDate + " ���ڼ����·ݣ�"  
                + formatDate(getSeasonDate(date)[0], "yyyy��MM��") + "/"  
                + formatDate(getSeasonDate(date)[1], "yyyy��MM��") + "/"  
                + formatDate(getSeasonDate(date)[2], "yyyy��MM��"));  
    }  
}  

